<?php
/**
 * Created by PhpStorm.
 * User: 刘红波 <liuhb@cqjcdflzxsws.com>
 * Date: 2018/8/30
 * Time: 10:33
 */

namespace app\sd\admin;


use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\sd\model\TopicDetail;

class Detail extends Admin
{
    public function index(){
        $map = $this->getMap();
        $model = new TopicDetail();
        $map = array_merge($map,['u.status'=>1,'u.is_delete'=>0]);
        $data_list = $model->alias('d')
            ->join('__WE_USERS__ u','d.user_id = u.id')
            ->field('d.id,d.user_id,d.content,d.topic_id,d.create_time,d.is_delete,u.we_nick_name')
            ->where($map)
            ->paginate();
        $columns = [
            ['id','ID'],
            ['content','评论内容'],
            ['topic_id','话题ID'],
            ['user_id','评论用户ID'],
            ['we_nick_name','用户小程序昵称'],
            ['create_time','评论时间','datetime'],
            ['is_delete','是否删除','switch']
        ];
        return ZBuilder::make('table')
            ->setSearch(['topic_id'=>'话题ID'])
            ->addTopSelect('is_delete','是否删除',['0'=>'未删除','1'=>'已删除'])
            ->addColumns($columns)
            ->setRowList($data_list)
            ->setSearch(['d.topic_id'=>'话题ID','u.we_nick_name'=>'用户小程序内昵称'])
            ->setTableName('we_topic_detail')
            ->fetch();
    }


    /**
     * @param array $record
     * @return mixed|void
     */
    public function delete($record = [])
    {
        $id = input('ids');
        $handled = input('is_handled');
        $back = input('back');
        $back_decode = explode('.',$back);
        if (!$back_decode){
            $back = 'index';
        }
        if ($handled){
            $res = TopicDetail::update(['is_delete'=>1,'is_handled'=>1],['id'=>$id]);
            if ($res){
                return $this->success('删除成功',url($back_decode[0].'/'.$back_decode[1]));
            }else{
                return $this->error('删除失败',url($back_decode[0].'/'.$back_decode[1]));
            }
        }else{
            return parent::delete($record); // TODO: Change the autogenerated stub
        }
    }
}